package kstech.com.wifitest.service;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.SystemClock;

import java.util.List;

import kstech.com.wifitest.Point;
import kstech.com.wifitest.utils.AppContext;

/**
 * Created by lijie on 2017/5/24.
 */

public class DialogAsyncTask extends AsyncTask<List<Point>,String,Void> {
    private Context context;
    // 弹出窗口
    private ProgressDialog mProgressDialog = null;

    public DialogAsyncTask(Context context) {
        this.context = context;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(context);
        // 设置进度条风格，风格为圆形，旋转的
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

        // 设置ProgressDialog 提示信息
        mProgressDialog.setMessage("正在储存，请稍等。。。");

        // 设置ProgressDialog 的进度条是否不明确
        mProgressDialog.setIndeterminate(false);
        // 设置ProgressDialog 是否可以按退回按键取消
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();
    }
    @Override
    protected Void doInBackground(List<Point>... params) {
        String result = AppContext.storePoint(params[0],params[1]);
        SystemClock.sleep(2000);
        if (result != null){
            publishProgress("保存成功，路径："+result);
        }else {
            publishProgress("保存失败，请重试");
        }
        SystemClock.sleep(1000);

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        mProgressDialog.cancel();
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
        mProgressDialog.setMessage(values[0]);
    }
}
